(function( $, undefined ) {

$.widget("ui.ajajform_widget", {
	options: {
	},
	
	_create: function() {
	},
	
	_setOption: function(key,value) {
		this.options[key]=value;
		var k1=key.substr(0,1);
		var k2=key.substr(1);
		this["refresh"+k1.toUpperCase()+k2]();
	},
	
	refreshError: function(e) {
		$(".af-error",this.element).text(e==null?"":e);
	},
	
	refreshLabel: function() {
		$(".af-label",this.element).text(this.options.label);
	},
	
	tmpl: function(tmpl,hash) {
		tmpl=tmpl.replace(/{{(\w+)}}/g,function(p,p1) { return (p1 in hash)?hash[p1]:""; });
		return tmpl;
	},
	
	gettext: function(name) {
		var l=ajajform_global_options.language;
		if (!this.i18n) return name;
		if (!this.i18n[l]) return name;
		if (!this.i18n[l][name]) return name;
		return this.i18n[l][name];
	},
	
	genericCreate: function(func) {
		if (typeof this.options.cssclass =="string") {
			this.element.addClass(this.options.cssclass);
		} else {
			for (var i=0,l=this.options.cssclass.length;i<l;i++) this.element.addClass(this.options.cssclass[i]);
		}
		if (this.options.disableable && this.options.htmltemplate_disableable) {
			var t=this.options.htmltemplate_disableable;
			if (func) t=func(t);
			this.element.append(t);
			$(".af-disable",this.element).bind("click",{t:this},function(e) {
				e.data.t._setOption("disabled",!e.data.t.options.disabled);
				e.data.t.refreshDisabled();
			});
			$(".af-label",this.element).width(180);
		} else {
			var t=this.options.htmltemplate;
			if (func) t=func(t);
			this.element.append(t);
		}
	},
	
	openDialog: function(dialogoptions,widgetslist,hash) {
		var widgets=[];
		for (var i=0;i<widgetslist.length;i++) {
			var w={};
			widgets.push(w);
			for (var k in widgetslist[i]) {
				var v=widgetslist[i][k];
				if (typeof v=="string") {
					// TODO TODO TODO Incorrect but works for now
					var a=v.match(/^\{\{(\w+)(?:\.(\w+))*\}\}$/);
					if (a) {
						var tmp=hash;
						var ii=1;
						while (ii<a.length && tmp && a[ii]!=undefined) {
							tmp=tmp[a[ii]];
							ii++;
						}
						w[k]=tmp;
					} else {
						w[k]=v;
					}
				} else {
					w[k]=v;
				}
			}
		}
		var frmoptions={
			header: null
		};
		if (dialogoptions.footerButtons) frmoptions.footerButtons=dialogoptions.footerButtons;
		return ajajform_dialog(dialogoptions,frmoptions,widgets);
	},

	genericRefreshDisabled: function() {
		if (!this.options.disableable) return;
		if (this.options.disabled) {
			$(".af-disable",this.element).addClass("af-disabled");
			$(".af-edit input",this.element).addClass("af-edit-disabled");
			$(".af-edit select",this.element).addClass("af-edit-disabled");
			$(".af-edit textarea",this.element).addClass("af-edit-disabled");
		} else {
			$(".af-disable",this.element).removeClass("af-disabled");
			$(".af-edit input",this.element).removeClass("af-edit-disabled");
			$(".af-edit select",this.element).removeClass("af-edit-disabled");
			$(".af-edit textarea",this.element).removeClass("af-edit-disabled");
		}
	},
	
	postvalue: function(r1,r2) {
	},
	
	destroy: function() {
	},
	
	validate: function() {
		return null;
	}

});

})( jQuery );

